package com.bdqn.bus.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.bus.entity.Car;
import com.bdqn.bus.service.CarService;
import com.bdqn.bus.vo.CarVo;
import com.bdqn.common.utitls.DateGindViewResult;
import com.bdqn.common.utitls.JSONResult;
import com.bdqn.common.utitls.SystemConstants;
import com.bdqn.sys.controller.utils.FileUploadUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.FilenameFilter;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author KazuGin
 * @since 2020-05-10
 */
@RestController
@RequestMapping("/bus/car")
public class CarController {

        @Resource
        private CarService carService;


        @RequestMapping("/list")
        public DateGindViewResult list(CarVo carvo){
                IPage<Car> page=new Page<Car>();
                QueryWrapper<Car> queryWrapper=new QueryWrapper<Car>();
                queryWrapper.like(StringUtils.isNotBlank(carvo.getCartype()),"cartype",carvo.getCartype());
                queryWrapper.like(StringUtils.isNotBlank(carvo.getCarnumber()),"carnumber",carvo.getCarnumber());
                queryWrapper.like(StringUtils.isNotBlank(carvo.getColor()),"color",carvo.getColor());
                queryWrapper.eq(carvo.getIsrenting()!=null,"isrenting",carvo.getIsrenting());//是否出租
                queryWrapper.ge(carvo.getStartPrice()!=null,"rentprice",carvo.getStartPrice());//最低价格
                queryWrapper.le(carvo.getEndPrice()!=null,"rentprice",carvo.getEndPrice());//最高价格


                carService.page(page,queryWrapper);

            return new DateGindViewResult(page.getTotal(),page.getRecords());
        }



        /**
         * 车辆添加
         */
        @RequestMapping("/addCar")
        public JSONResult addCustomer(Car car){

                if (car!=null&&car.getCarimg()!=null&&car.getCarimg().length()>0&&car.getCarimg().endsWith("_temp")){
                        //成立就修改文件名
                        String newFileName= FileUploadUtils.renameFile(car.getCarimg());
                        //设置文件名
                        car.setCarimg(newFileName);
                }
                car.setCreatetime(new Date());//录入时间
                if(carService.save(car)){
                        return SystemConstants.Add_SUCCESS;
                }
                return SystemConstants.Add_ERROR;
        }


        /**
         * 更新修改车辆
         * @return
         */
        @RequestMapping("/updateCar")
        public JSONResult updateCar(Car car){
                //判断对象是否为空
             if (car!=null){
                //判断当前的车辆是否是默认图片，默认图片不需要改后缀
                     if (!car.getCarimg().equals(SystemConstants.DEFAULT_CAR_IMG)){//不是默认图片defaultimg.jpg
                        //判断文件是否存在后缀名
                             if (car.getCarimg().endsWith("_temp")){
                                 //重新修改名字
                                 String newFileName= FileUploadUtils.renameFile(car.getCarimg());
                                 //设置文件名
                                 car.setCarimg(newFileName);
                                 //旧文件需要删除
                                 String oldImg=carService.getById(car.getId()).getCarimg();
                                 //删除
                                 FileUploadUtils.deleteFile(oldImg);
                             }

                     }
                 if(carService.updateById(car)){
                     return SystemConstants.UPDATE_SUCCESS;
                 }

             }
                 return SystemConstants.UPDATE_ERROR;
        }

    /**
     * 删除
     * @param id
     * @param carimg
     * @return
     */
        @RequestMapping("/deleteById")
        public  JSONResult deleteById(Integer id,String carimg){
            if (carService.removeById(id)){
                //删除图片
                FileUploadUtils.deleteFile(carimg);
                return SystemConstants.DELETE_SUCCESS;
            }
            return  SystemConstants.DELETE_ERROR;
        }

    /**
     * 根据车牌号查询汽车的信息
     * @param carnumber
     * @return
     */
    @RequestMapping("/getCarInfoByCarnumber")
        public  String getCarInfoByCarNumber(String carnumber){
            QueryWrapper<Car> queryWrapper=new QueryWrapper<Car>();
            queryWrapper.eq("carnumber",carnumber);
            Car car = carService.getOne(queryWrapper);
            return JSON.toJSONString(car);
        }





}

